package com.mdd.maduoduo.activity;

import android.content.Intent;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.mdd.maduoduo.BuildConfig;
import com.mdd.maduoduo.R;
import com.mdd.maduoduo.constants.CommonConstants;
import com.mdd.maduoduo.di.component.DaggerUIComponent;
import com.mdd.maduoduo.di.component.UIComponent;
import com.mdd.maduoduo.entity.Response;
import com.mdd.maduoduo.presenter.LoginPresenter;
import com.mdd.maduoduo.utils.AlertUtils;
import com.mdd.maduoduo.utils.AppManager;
import com.mdd.maduoduo.utils.ClipeBoardUtil;
import com.mdd.maduoduo.utils.SpUtils;
import com.mdd.maduoduo.utils.ToastUtils;
import com.mdd.maduoduo.utils.Utils;
import com.mdd.maduoduo.view.interfaces.LogoutView;

import javax.inject.Inject;

public class SettingActivity extends BaseActivity implements LogoutView {

    private TextView tvLogout,tvDelete,tvCode,tvAppVersion,tvId,tvNickName;

    private RelativeLayout rlNickName,rlId,rlCode;

    @Inject
    public LoginPresenter mPresenter;

    private ImageView ivBack;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_setting;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mPresenter.onDestroy();
    }

    @Override
    protected void initializeInjector() {
        super.initializeInjector();
        mPresenter = new LoginPresenter();
        UIComponent uiComponent = DaggerUIComponent.builder().applicationComponent(getApplicationComponent())
                .activityModule(getActivityModule()).build();
        uiComponent.inject(this);
        mPresenter.setLogoutView(this);
    }

    @Override
    protected void initView() {
        super.initView();
        tvLogout = findViewById(R.id.tvLogout);
        tvDelete = findViewById(R.id.tvDelete);
        tvCode = findViewById(R.id.tvCode);
        tvAppVersion = findViewById(R.id.tvAppVersion);
        tvId = findViewById(R.id.tvId);
        tvNickName = findViewById(R.id.tvNickName);
        rlNickName = findViewById(R.id.rlNickName);
        rlId = findViewById(R.id.rlId);
        rlCode= findViewById(R.id.rlCode);
        ivBack = findViewById(R.id.ivBack);

        Intent intent = getIntent();
        tvNickName.setText(intent.getStringExtra("nick"));
        tvId.setText(intent.getStringExtra("id"));
        tvCode.setText(intent.getStringExtra("pickupSharedCode"));
        tvAppVersion.setText(BuildConfig.VERSION_NAME);

        rlNickName.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ClipeBoardUtil.setClipeBoardContent(SettingActivity.this,tvNickName.getText().toString());
                ToastUtils.showLongToast(SettingActivity.this,"复制成功");
            }
        });

        rlId.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ClipeBoardUtil.setClipeBoardContent(SettingActivity.this,tvId.getText().toString());
                ToastUtils.showLongToast(SettingActivity.this,"复制成功");
            }
        });

        rlCode.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ClipeBoardUtil.setClipeBoardContent(SettingActivity.this,tvCode.getText().toString());
                ToastUtils.showLongToast(SettingActivity.this,"复制成功");
            }
        });
    }

    @Override
    protected void onClick() {
        ivBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        tvLogout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (Utils.isFastClick()){
                    AlertUtils.normalAlert(SettingActivity.this, "温馨提示", "确定要退出账号吗?", new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            mPresenter.logout(SettingActivity.this);
                            AlertUtils.dismiss();
                        }
                    });
                }
            }
        });

        tvDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (Utils.isFastClick()){
                    AlertUtils.normalAlert(SettingActivity.this, "温馨提示", "确定要删除账号吗?", new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            mPresenter.logout(SettingActivity.this);
                            AlertUtils.dismiss();
                        }
                    });
                }
            }
        });
    }

    @Override
    public void renderSuccess(Response response) {
        //退出登录成功
        SpUtils.put(SettingActivity.this, CommonConstants.TOKEN,"");
        AppManager.getAppManager().finishAllActivity();
        startActivity(new Intent(SettingActivity.this,LoginActivity.class));
    }
}
